<!-- 用户资料：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex">
          <h1 class="f16">用户管理</h1>
          <JDialog
            :tableHeader="tableHeader"
            :width="120"
            @removeOne="removeOne"
            :currentTableRow="currentTableRow"
            @confirm="confirm"
            :unEdit="false"
          />
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'SubscriberDataKla'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入中文名',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入部门名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入职务名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '重置密码'
  }
]) as any
// 表格数据
const tableHeader = ref([
  {
    label: '公司',
    prop: 'abbrName'
  },
  {
    label: '用户代码',
    prop: 'userId'
  },
  {
    label: '英文名',
    prop: 'engName'
  },
  {
    label: '中文名',
    prop: 'chiName',
    width: '60px'
  },
  {
    label: '部门',
    prop: 'deptName',
    width: '207px',
    type: 'selectJ'
  },
  {
    label: '职务',
    prop: 'posName',
    width: '207px',
    type: 'selectJ'
  },
  {
    label: '最后登陆时间',
    prop: 'lastLogin',
    width: '158px'
  },
  {
    label: '锁定状态',
    prop: 'lockStatus',
    width: '112px',
    type: 'selectJ'
  },
  {
    label: '领用部门',
    prop: 'drawDept',
    width: '112px',
    type: 'selectJ'
  },
  {
    label: '派工部门',
    prop: 'dispatchDept',
    width: '112px',
    type: 'selectJ'
  },
  {
    label: '外网标记',
    prop: 'netFlag',
    width: '112px',
    type: 'selectJ'
  },
  {
    label: '备注',
    prop: 'remark',
    width: '304px'
  }
])
// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/sdUser/getUser', {
    chiName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入中文名字'
    )?.inputValue,
    deptName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入部门名称'
    )?.inputValue,
    posName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入职务名称'
    )?.inputValue,
    lockStatus: '', //锁定状态 Y锁定 N正常 默认为空
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}

const JTableHeader = ref([
  {
    label: '公司',
    prop: 'abbrName'
  },
  {
    label: '用户代码',
    prop: 'userId'
  },
  {
    label: '英文名',
    prop: 'engName'
  },
  {
    label: '中文名',
    prop: 'chiName',
    width: '60'
  },
  {
    label: '部门',
    prop: 'deptName',
    width: '207',
    type: 'selectJ'
  },
  {
    label: '职务',
    prop: 'posName',
    width: '207',
    type: 'selectJ'
  },
  {
    label: '最后登陆时间',
    prop: 'lastLogin',
    width: '158'
  },
  {
    label: '锁定状态',
    prop: 'lockStatus',
    width: '112',
    type: 'selectJ'
  },
  {
    label: '领用部门',
    prop: 'drawDept',
    width: '112',
    type: 'selectJ'
  },
  {
    label: '派工部门',
    prop: 'dispatchDept',
    width: '112',
    type: 'selectJ'
  },
  {
    label: '外网标记',
    prop: 'netFlag',
    width: '112',
    type: 'selectJ'
  },
  {
    label: '备注',
    prop: 'remark',
    width: '304'
  }
])
// Dialog确认添加/修改按钮
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await LoginWork.post(`/sdUser/addUser`, {
      ...currentTableRow.value,
      chiName: obj.chiName.value,
      deptNo: obj.deptNo.value,
      engName: obj.engName.value,
      posId: obj.posId.value,
      remark: obj.remark.value
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
      getTableList()
      dialogVisible.value = false
    } else {
      JMessage.error(res.message)
    }
  } else if (title === '修改') {
    const res = (await LoginWork.post(`/sdUser/updateUser`, {
      ...currentTableRow.value,
      chiName: obj.chiName.value,
      deptNo: obj.deptNo.value,
      engName: obj.engName.value,
      posId: obj.posId.value,
      remark: obj.remark.value
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
      getTableList()
      dialogVisible.value = false
    } else {
      JMessage.error(res.message)
    }
  }
}
// 删除
const removeOne = async () => {
  const res = (await LoginWork.post(`/sdUser/delUser?userId=${currentTableRow.value.userId}`)) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.message)
    getTableList()
  } else {
    JMessage.error(res.message)
  }
}
</script>

<style lang="less" scoped></style>
